package com.bruce.starts.demo.csv;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.csv.CsvData;
import cn.hutool.core.text.csv.CsvReader;
import cn.hutool.core.text.csv.CsvRow;
import cn.hutool.core.text.csv.CsvUtil;
import cn.hutool.core.util.CharsetUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class CsvReaderDemo {
    public static void main(String[] args) {
        read();

    }

    private static void read(){
        CsvReader reader = CsvUtil.getReader();
        //从文件中读取CSV数据
        CsvData data = reader.read(FileUtil.file("F:\\致景科技\\03_开发\\03_开发任务\\03_进销存系统\\sm2.7.0\\品种编号刷数\\工厂_品种.csv"), CharsetUtil.CHARSET_GBK);
        List<CsvRow> rows = data.getRows();

        for (int i = 0; i < rows.size(); i++) {
           if(i==0){
               continue;
           }
            CsvRow row = rows.get(i);
            List<String> rawList = row.getRawList();
            if(StringUtils.isNotBlank(rawList.get(3))){
                //System.out.println("select * from woven_conf.base_variety where factory_id ='"+rawList.get(0)+"' and variety_num ='"+rawList.get(3)+"';");
               System.out.println("update woven_conf.base_variety set variety_num='"+rawList.get(3)+"' where factory_id ='"+rawList.get(0)+"' and variety_name ='"+rawList.get(2)+"';");
            }
        }
    }

    //private static void read01(){
    //    String csvFile = "F:\\致景科技\\03_开发\\03_开发任务\\03_进销存系统\\sm2.7.0\\品种编号刷数\\工厂_品种.csv";
    //    String line = "";
    //    String cvsSplitBy = ",";
    //
    //    try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
    //
    //        while ((line = br.readLine()) != null) {
    //
    //            // use comma as separator
    //            String[] country = line.split(cvsSplitBy);
    //
    //            System.out.println("Country [code= " + country[4] + " , name=" + country[5] + "]");
    //
    //        }
    //
    //    } catch (IOException e) {
    //        e.printStackTrace();
    //    }
    //}
    //
    //public static void read02(){
    //
    //    try (Reader reader = Files.newBufferedReader(Paths.get("F:\\致景科技\\03_开发\\03_开发任务\\03_进销存系统\\sm2.7.0\\品种编号刷数\\工厂_品种.csv"))) {
    //        Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(reader);
    //        for (CSVRecord record : records) {
    //            System.out.println("Record #: " + record.getRecordNumber());
    //            System.out.println("ID: " + record.get(0));
    //            System.out.println("Name: " + record.get(1));
    //            System.out.println("Email: " + record.get(2));
    //            System.out.println("Country: " + record.get(3));
    //        }
    //    } catch (IOException ex) {
    //        ex.printStackTrace();
    //    }
    //}
}